package wnhz.com.business;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.changepwd.ChangePwd1Activity;
import wnhz.com.business.forget.AmentPasswordActivity;
import wnhz.com.business.groupshop.bean.InfoBean;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.shareshop.ShareShopActivity;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.StatusBarUtil;
import wnhz.com.business.uitls.Url;

/**
 * Created by lizhenglong on 2017-10-23 0023.
 */

public class LoginActivity extends MyBaseActivity {
    private static final String TAG = "LoginActivity";
    @BindView(R.id.v_statusbar)
    View vStatusbar;
    @BindView(R.id.rl_login_pwd)//密码登录
    RelativeLayout rl_login_pwd;
    @BindView(R.id.title_pwdlogin)//密码登录标题
    TextView title_pwdlogin;
    @BindView(R.id.tv_title_line1)//密码登录_下划线
    TextView tv_titleLine1;
    @BindView(R.id.rl_login_sms)//短信登录
    RelativeLayout rl_login_sms;
    @BindView(R.id.title_smslogin)//短息登录标题
    TextView title_smslogin;
    @BindView(R.id.tv_title_line2)//短信登录_下划线
    TextView tv_titleLine2;

    @BindView(R.id.tv_loginchose_type)//界面显示_登录类型
    TextView tv_loginchoseType;

    @BindView(R.id.et_loginpwd_phone)//密码登录_输入手机号
    EditText et_loginpwdPhone;
    @BindView(R.id.rl_loginpwd_tel)//密码登录_输入手机号
    RelativeLayout rl_loginpwdTel;
    @BindView(R.id.ed_loginpwd_pwd)//密码登录_输入密码
    EditText ed_loginpwdPwd;
    @BindView(R.id.img_hide_pwd)//密码登录_隐藏显示密码
    ImageView img_hidePwd;
    @BindView(R.id.rl_loginpwd_pwd)//密码登录_输入密码
    RelativeLayout rl_loginpwdPwd;
    @BindView(R.id.retrieve_password)//密码登录_找回密码
    TextView retrieve_password;

    @BindView(R.id.et_loginsms_phone)//短信登录_输入手机号码
    EditText et_loginsmsPhone;
    @BindView(R.id.rl_loginsms_tel)//短信登录_输入手机号码
    RelativeLayout rl_loginsmsTel;
    @BindView(R.id.ed_loginsms_code)//短信登录输入输入验证码
    EditText ed_loginsmsCode;
    @BindView(R.id.img_loginsms_code)//获取验证码
    TextView img_loginsmsCode;
    @BindView(R.id.rl_loginsms_code)//短信登录_输入验证码
    RelativeLayout rl_loginsmsCode;
    @BindView(R.id.btn_login)//登录
    Button btn_login;
    @BindView(R.id.tv_login_register)//注册
    TextView tv_loginRegister;


    private int typeflag = 1;
    private boolean pwdflag = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        if (!TextUtils.isEmpty(Prefer.getInstance().getToken())){
            Intent intent = new Intent(LoginActivity.this, ShareShopActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void initView() {
        //状态栏设置
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.transparent), 0);
        StatusBarUtil.setDarkMode(this, true);
        StatusBarUtil.immersive(this, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
////            //透明导航栏
////            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
////            //改变状态栏颜色
////            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            vStatusbar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vStatusbar.getLayoutParams();
            params.height = statusHeight;
            vStatusbar.setLayoutParams(params);
        }

        MyApplication.getInstance().addActivity(this);

        retrieve_password.setVisibility(View.VISIBLE);//密码登录_找回密码_显示



    }

    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what){
            case 101://倒計時
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(img_loginsmsCode, 60000, 1000);
                mCountDownTimerUtils.start();
                break;
        }
    }
//    R.id.rl_login_back,
    @OnClick({R.id.rl_login_pwd,R.id.rl_login_sms,R.id.img_loginsms_code,R.id.tv_login_register
    ,R.id.img_hide_pwd,R.id.btn_login,R.id.retrieve_password})
    void click(View view){
        Intent intent = null;
        switch (view.getId()){
//            case R.id.rl_login_back://返回
//                intent = new Intent(LoginActivity.this,BeginActivity.class);
//                startActivity(intent);
//                finish();
//                break;
            case R.id.rl_login_pwd://点击密码登录
                typeflag = 1;
                title_pwdlogin.setTextColor(getResources().getColor(R.color.red2552));
                tv_titleLine1.setTextColor(getResources().getColor(R.color.red2552));
                tv_titleLine1.setVisibility(View.VISIBLE);
                title_smslogin.setTextColor(getResources().getColor(R.color.text_gray));
                tv_titleLine2.setVisibility(View.GONE);
                tv_loginchoseType.setText("密码登录");
                rl_loginpwdTel.setVisibility(View.VISIBLE);
                rl_loginpwdPwd.setVisibility(View.VISIBLE);
                retrieve_password.setVisibility(View.VISIBLE);
                rl_loginsmsTel.setVisibility(View.GONE);
                rl_loginsmsCode.setVisibility(View.GONE);
                break;
            case R.id.rl_login_sms://点击短信登录
                typeflag = 2;
                title_pwdlogin.setTextColor(getResources().getColor(R.color.text_gray));
                tv_titleLine1.setVisibility(View.GONE);
                title_smslogin.setTextColor(getResources().getColor(R.color.red2552));
                tv_titleLine2.setTextColor(getResources().getColor(R.color.red2552));
                tv_titleLine2.setVisibility(View.VISIBLE);
                tv_loginchoseType.setText("短信登录");
                rl_loginpwdTel.setVisibility(View.GONE);
                rl_loginpwdPwd.setVisibility(View.GONE);
                rl_loginsmsTel.setVisibility(View.VISIBLE);
                rl_loginsmsCode.setVisibility(View.VISIBLE);
                retrieve_password.setVisibility(View.GONE);
                break;
            case R.id.img_hide_pwd://隐藏显示密码
                if (pwdflag) {
                    pwdflag = false;
                    img_hidePwd.setImageResource(R.drawable.ic_login_show);//设置显示密码的图片
                    ed_loginpwdPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ed_loginpwdPwd.setSelection(ed_loginpwdPwd.getText().toString().length()); //调整光标的位置到最后
                } else {
                    pwdflag = true;
                    img_hidePwd.setImageResource(R.drawable.ic_login_notshow);//设置隐藏密码的图片
                    ed_loginpwdPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ed_loginpwdPwd.setSelection(ed_loginpwdPwd.getText().toString().length());
                }
                break;
            case R.id.img_loginsms_code://获取验证码
                getLoginCode();
                break;
            case R.id.tv_login_register://立即注册

                intent = new Intent(LoginActivity.this,RegisterActivty.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                login();

                break;
            case R.id.retrieve_password:
                intent = new Intent(LoginActivity.this, ChangePwd1Activity.class);
                intent.putExtra("type","3");
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void login() {
        Map<String,String> map = new HashMap<>();
        if (typeflag == 1){//密码登录
            map.put("mobile", Base64Util.encodedString2(et_loginpwdPhone.getText().toString()));
            map.put("password",Base64Util.encodedString2(ed_loginpwdPwd.getText().toString()));
            map.put("type","1");
        }else {//短信登录
            map.put("mobile",Base64Util.encodedString2(et_loginsmsPhone.getText().toString()));
            map.put("code",ed_loginsmsCode.getText().toString());
            map.put("type","2");
        }
        OkGo.<InfoBean>post(Url.USER_LOGIN)
                .tag(this)
                .params(map,false)
                .execute(new DialogCallback<InfoBean>(this) {
                    @Override
                    public void onSuccess(Response<InfoBean> response) {
                        if (response.body().getRe().equals("1")){
                            Log.e("登录onSuccess",""+response.body().getInfo());
                            InfoBean infoBean = new InfoBean();
                            if (typeflag == 1){//密码登录
//                                SharedPreferences sp = getSharedPreferences("userinfo", 0);
//                                SharedPreferences.Editor editor = sp.edit();
//                                editor.putString("username", et_loginpwdPhone.getText().toString());
//                                editor.putString("password", ed_loginpwdPwd.getText().toString());
//                                editor.putString("token", response.body().getToken());
//                                editor.putString("logintype", "1");
//                                editor.commit();
//                                infoBean.setMobile(et_loginpwdPhone.getText().toString());
//                                infoBean.setPassword(ed_loginpwdPwd.getText().toString());
//                                infoBean.setToken(response.body().getToken());
//                                infoBean.setLogintype("1");

//                                Prefer.getInstance().setName(et_loginpwdPhone.getText().toString());
                                Prefer.getInstance().setToken(response.body().getToken());
                                Prefer.getInstance().setPhone(et_loginpwdPhone.getText().toString());
                                Prefer.getInstance().setUser_id(infoBean.getUser_id());
                                MyApplication.setToken(response.body().getToken());
//                                Prefer.getInstance().setloginTypeS("1");
                            }else {//短信登录
//                                SharedPreferences sp = getSharedPreferences("userinfo", 0);
//                                SharedPreferences.Editor editor = sp.edit();
//                                editor.putString("username", et_loginpwdPhone.getText().toString());
//                                editor.putString("token", response.body().getToken());
//                                editor.putString("logintype", "2");
//                                editor.commit();
//                                infoBean.setMobile(et_loginpwdPhone.getText().toString());
//                                infoBean.setToken(response.body().getToken());
//                                infoBean.setLogintype("2");

                                Prefer.getInstance().setPhone(et_loginpwdPhone.getText().toString());
                                Prefer.getInstance().setToken(response.body().getToken());
                                MyApplication.setToken(response.body().getToken());
                                Prefer.getInstance().setUser_id(infoBean.getUser_id());
                                Prefer.getInstance().setloginTypeS("2");


                            }
                            Intent intent = new Intent(LoginActivity.this, ShareShopActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            MyToast(response.body().getInfo());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<InfoBean> response) {
                        super.onError(response);
                    }
                });
    }

    private void getLoginCode() {
        //    获取验证码
        //    @param Str    code      接口请求密码  UNOHACHA
        //    @param Int    mobile    手机号   （加密）
        //    @param Int    type      1 注册  2 登录  3 忘记密码   4 更换手机号  5 修改交易密码  6 修改登录密码  7三方登陆  8绑定银行卡
        //    syb.unohacha.com/Api/Api/User_getMessage
        Map<String,String> map = new HashMap<>();
        map.put("code", Url.CODE);
        map.put("mobile", Base64Util.encodedString2(et_loginsmsPhone.getText().toString()));
        map.put("type", "2");
        Log.e("获取验证码", "" + Url.REGISTER_GETCODE);
        OkGo.<InfoBean>post(Url.REGISTER_GETCODE)
                .tag(this)
                .params(map,false)
                .execute(new DialogCallback<InfoBean>(this) {
                    @Override
                    public void onSuccess(Response<InfoBean> response) {
                        Log.e("REGISTER_GETCODE", "" + response);
                        if (response.body().getRe().equals("1")) {
                            handler.sendEmptyMessage(101);
                            MyToast(response.body().getInfo());
                        }
                    }

                    @Override
                    public void onFinish() {
                            super.onFinish();
                        }

                    @Override
                    public void onError(Response<InfoBean> response) {
                        super.onError(response);
                    }
                });
    }


}
